﻿using APH.Parl2.DataAccess;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using APH.Parl2.DataAccess.ReadModel;
using System.Collections.Generic;
using System.Linq;

namespace TestProject
{
    
    
    /// <summary>
    ///This is a test class for DataAccessHelperTest and is intended
    ///to contain all DataAccessHelperTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ReadHelperTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GetDocumentById
        ///</summary>
        [TestMethod()]
        public void GetDocumentByIdTest()
        {
            var Id = ReadHelper.CreateDocument("John Smith", "<xml/>", "title");
            var expectedId = Id;
            var actual = ReadHelper.GetDocumentById(Id);
            Assert.AreEqual(expectedId, actual.Id);
        }

        /// <summary>
        ///A test for CreateDocument
        ///</summary>
        [TestMethod()]
        public void CreateDocumentTest()
        {
            var author = "Author"; 
            var data = "<xml/>";
            var title = "title";
            var actual = ReadHelper.CreateDocument(author, data, title);
            var doc = ReadHelper.GetDocumentById(actual);
            Assert.AreEqual(doc.Id, actual);
        }

        /// <summary>
        ///A test for DeleteDocument
        ///</summary>
        [TestMethod()]
        public void DeleteDocumentTest()
        {
            var id = ReadHelper.CreateDocument("John Smith", "<xml/>", "title");
            ReadHelper.DeleteDocument(id);
            var doc = ReadHelper.GetDocumentById(id);
            Assert.IsNull(doc);
        }

        /// <summary>
        ///A test for UpdateDocumentContent
        ///</summary>
        [TestMethod()]
        public void UpdateDocumentContentTest()
        {
            var id = ReadHelper.CreateDocument("John Smith", "<xml/>", "title");
            string data = "<xml></xml>";
            ReadHelper.UpdateDocumentContent(id, data);
            var doc = ReadHelper.GetDocumentById(id);
            Assert.AreEqual(doc.Data, data);
        }

        /// <summary>
        ///A test for GetDocumentsByTitle
        ///</summary>
        [TestMethod()]
        public void GetDocumentsByTitleTest()
        {
            string title = "Some Title";
            var id1 = ReadHelper.CreateDocument("", "", title);
            var id2 = ReadHelper.CreateDocument("", "", title);
            var expected = 2;
            try
            {
                IEnumerable<Document> actual = ReadHelper.GetDocumentsByTitle(title);
                Assert.AreEqual(expected, actual.ToList().Count);
            }
            finally
            {
                ReadHelper.DeleteDocument(id1);
                ReadHelper.DeleteDocument(id2);
            }
        }

        /// <summary>
        ///A test for GetDocumentsByAuthor
        ///</summary>
        [TestMethod()]
        public void GetDocumentsByAuthorTest()
        {
            string author = "Daniel";
            var id1 = ReadHelper.CreateDocument(author, "", "");
            var id2 = ReadHelper.CreateDocument(author, "", "");
            var expected = 2;
            try
            {
                IEnumerable<Document> actual = ReadHelper.GetDocumentsByAuthor(author);
                Assert.AreEqual(expected, actual.ToList().Count);
            }
            finally
            {
                ReadHelper.DeleteDocument(id1);
                ReadHelper.DeleteDocument(id2);
            }
        }

        /// <summary>
        ///A test for ConnectionString
        ///</summary>
        [TestMethod()]
        public void ConnectionStringTest()
        {
            string expected = "metadata=res://*/WriteModel.WriteModel.csdl|res://*/WriteModel.WriteModel.ssdl|res://*/WriteModel.WriteModel.msl;provider=System.Data.SqlClient;provider connection string=\"Data Source=localhost;Initial Catalog=Parl2_Write;Integrated Security=True;MultipleActiveResultSets=True\"";
            string actual = WriteHelper.ConnectionString;
            Assert.AreEqual(expected, actual);
        }
    }
}
